﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Yugi.Client.Card;
using Yugi.Client.Control;
using Yugi.Client;

namespace Yugi.Client
{
    public class CardChoiceHandDoiThu
    {
        public int Count { get; set; }
        public List<HandImage> Data { get; set; }
        public bool IsProcess { get; set; }
        
        public List<int> Choices { get; set; }
        public YType SelectionType { get; set; }
        public event EventHandler OnChoiceComplete;
        public event Action<object,string> OnError;
        public CardChoiceHandDoiThu()
        {
            IsProcess = false;
            Choices = new List<int>();
            Last = new Dictionary<int, HandImage.State>();
            SelectionType = YType.Monster;
        }
        public void Begin()
        {
            Choices.Clear();
            Last.Clear();
            int dem = 0;
            foreach (var item in Data)
            {
                HandImage card = item;
                card.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(card_MouseLeftButtonDown);
                dem++;
               
            }
            if(dem<Count)
                if (OnError != null)
                {
                    OnError(this,"Trên Tay Không Đủ Số Lượng Bài Cần Thiết!!!");
                    End();
                }
        }

        void card_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            HandImage card = sender as HandImage;
            int data = ((int)(card.DataContext));
            
            if (Choices.Contains(data))
            {
                Choices.Remove(data);
                card.CurrentState = Last[data];
            }
            else
            {
                Last[data] = card.CurrentState;
                card.CurrentState =  HandImage.State.Select_DoiThu;
                Choices.Add(data);
            }
            if (Choices.Count == Count)
            {
                if (OnChoiceComplete != null)
                    OnChoiceComplete(this, new EventArgs());
                End();
            }
        }
        public void End()
        {
            foreach (var item in Data)
            {
                HandImage card = item;
            
                    if (card.CurrentState ==  HandImage.State.Select_DoiThu)
                        card.CurrentState = Last[((int)card.DataContext)];
                    card.MouseLeftButtonDown -= new System.Windows.Input.MouseButtonEventHandler(card_MouseLeftButtonDown);
              
            }
        }
        private Dictionary<int, HandImage.State> Last;
    }
}
